package com.xinli.framework.security;

/**
 * @author: zhaoyongke
 * @create: 2024-05-30 10:47
 * @description: openId
 */


import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xinli.business.service.IFtMemberService;
import com.xinli.business.service.impl.FtMemberServiceImpl;
import com.xinli.common.core.domain.entity.SysUser;
import com.xinli.common.core.domain.model.LoginUser;
import com.xinli.common.enums.UserStatus;
import com.xinli.common.exception.ServiceException;
import com.xinli.common.utils.SecurityUtils;
import com.xinli.common.utils.StringUtils;
import com.xinli.framework.web.service.SysPermissionService;
import com.xinli.system.service.ISysUserService;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * 查询短信登录信息并封装为 UserDetails 这里可以抽取一个抽象类，权限加载和校验租户等逻辑交给父类处理
 */
@Service("phoneUserDetailsService")
@Slf4j
public class PhoneUserDetailsService implements UserDetailsService {
    @Resource
    private ISysUserService userService;



    @Resource
    private SysPermissionService permissionService;

    /**
     * loadUserByUsername
     *
     * @return LoginUser
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String object) throws UsernameNotFoundException {

        System.out.println(object+"手机号===================");
        JSONObject parse = JSONUtil.parseObj(object);
        String  phone = parse.getStr("phone");
        String  password = parse.getStr("password");

        List<SysUser> sysUsers = userService.selectUserByPhone(phone);
        if(Collections.isEmpty(sysUsers)){
            throw new InternalAuthenticationServiceException("phone：" + object + " 不存在");
        }
        SysUser sysUser = sysUsers.get(0);
        boolean b = SecurityUtils.matchesPassword(password, sysUser.getPassword());
        if(!b){
            throw new DisabledException("密码错误");
        }
        return createLoginUser(sysUser);
    }

    public UserDetails createLoginUser(SysUser user) {
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
    }

    public static void main(String[] args) {
        String uu="123456";
        String uu1="123456";
        String password = SecurityUtils.encryptPassword(uu);
        String password1 = SecurityUtils.encryptPassword(uu1);
        if(password.equals(password1)){
            System.out.println("==================");

        }else{
            System.out.println("==345656================");
        }
    }

}
